package cn.hxzy.teacher.mapper;

import cn.hxzy.common.domain.Direction;
import cn.hxzy.common.domain.SysSubject;
import cn.hxzy.common.domain.SysSubjectLabel;
import org.apache.ibatis.annotations.Delete;
import cn.hxzy.common.domain.SysSubjectLabel;
import cn.hxzy.common.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *
 * @author tyhxzy
 * @@ -17,116 +10,6 @@ import java.util.List;
 * @since 2024-12-14
 */
public interface SysSubjectMapper {
    // 新增题目
    @Insert({
            "<script>",
            "insert into sys_subject (",
            "subject_name,",
            "<if test='optionA != null'>option_a,</if>",
            "<if test='optionB != null'>option_b,</if>",
            "<if test='optionC != null'>option_c,</if>",
            "<if test='optionD != null'>option_d,</if>",
            "<if test='optionE != null'>option_e,</if>",
            "<if test='optionF != null'>option_f,</if>",
            "subject_answer,",
            "subject_type,",
            "subject_label,",
            "subject_major,",
            "subject_image,",
            "subject_lv,",
            "creator,",
            "reviser,",
            "changetime",
            ") values (",
            "#{subjectName},",
            "<if test='optionA != null'>#{optionA},</if>",
            "<if test='optionB != null'>#{optionB},</if>",
            "<if test='optionC != null'>#{optionC},</if>",
            "<if test='optionD != null'>#{optionD},</if>",
            "<if test='optionE != null'>#{optionE},</if>",
            "<if test='optionF != null'>#{optionF},</if>",
            "#{subjectAnswer},",
            "#{subjectType},",
            "#{subjectLabel},",
            "#{subjectMajor},",
            "#{subjectImage},",
            "#{subjectLv},",
            "#{creator},",
            "#{reviser},",
            "#{changetime}",
            "<trim suffixOverrides=','>",
            "</trim>",
            ")",
            "</script>"
    })
    Integer addSubject(SysSubject sysSubject);

    //查询题目标签
    @Select("select * from sys_subject_label")
    List<SysSubjectLabel> selectSubjectLabel();

    //查询专业名称
    @Select("select * from sys_direction")
    List<Direction> selectDirection();

    // 根据 subject_id 修改 SysSubject 记录
    @Update({
            "<script>",
            "UPDATE sys_subject ",
            "SET ",
            "<if test='subjectName != null'>subject_name = #{subjectName},</if>",
            "<if test='optionA != null'>option_a = #{optionA},</if>",
            "<if test='optionB != null'>option_b = #{optionB},</if>",
            "<if test='optionC != null'>option_c = #{optionC},</if>",
            "<if test='optionD != null'>option_d = #{optionD},</if>",
            "<if test='optionE != null'>option_e = #{optionE},</if>",
            "<if test='optionF != null'>option_f = #{optionF},</if>",
            "<if test='subjectAnswer != null'>subject_answer = #{subjectAnswer},</if>",
            "<if test='subjectType != null'>subject_type = #{subjectType},</if>",
            "<if test='subjectLabel != null'>subject_label = #{subjectLabel},</if>",
            "<if test='subjectMajor != null'>subject_major = #{subjectMajor},</if>",
            "<if test='subjectImage != null'>subject_image = #{subjectImage},</if>",
            "<if test='subjectLv != null'>subject_lv = #{subjectLv},</if>",
            "<if test='reviser != null'>reviser = #{reviser},</if>",
            "<if test='changetime != null'>changetime = #{changetime}</if>",
            "<where>",
            "<if test='subjectId != null'>subject_id = #{subjectId}</if>", // 这里不需要，因为已经在 WHERE 子句外
            "</where>",
            "</script>"
    })
    Integer updateSysSubject(SysSubject sysSubject);
//物理删除题目
//@Delete("DELETE FROM sys_subject WHERE subject_id = #{subjectId}")
//Integer deleteById(Long subjectId);
    //逻辑删除题目

    @Update("UPDATE sys_subject SET isdelete = 1 WHERE subject_id = #{subjectId}")
    Integer deleteById(Long subjectId);

    @Select("select subject_id, subject_name,option_a,option_b,option_c,option_d,option_e,option_f,subject_answer,subject_type,subject_label,subject_major,subject_image,subject_lv,creator,createtime,reviser " +
            "from sys_subject " +
            "where isdelete=0 " +
            "order by createtime desc " + // 在这里添加了空格
            "LIMIT #{start},#{pageSize}")
    List<SysSubject> selectquanbu(Integer start, Integer pageSize);


    //根据id查询标签
    @Select({
            "<script>",
            "select label_id, label_name, major_id ",
            "from sys_subject_label ",
            "<choose>",
            "<when test='id != null and id.size() > 0'>",
            "where label_id IN ",
            "<foreach item='item' index='index' collection='id' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<otherwise>",
            "where 1=0", // 或者返回空结果集
            "</otherwise>",
            "</choose>",
            "</script>"
    })
    List<SysSubjectLabel> syssubjectlabel(List<Integer> id);

    //根据id查询专业
    @Select("select direction_id, direction_name from sys_direction where direction_id=#{id}")
    Direction sysdirection(Integer id);

    //根据id查询用户
    @Select("select user_id,nick_name from sys_user where user_id=#{id}")
    User sysuser(Long id);

    @Select("select count(*) as total from sys_subject where isdelete=0")
    Integer selectzong();


    @Select("select subject_id, subject_name, option_a, option_b, option_c, option_d, option_e, option_f, subject_answer, subject_type, subject_label, subject_major, subject_image, subject_lv, creator, createtime, reviser " +
            "from sys_subject " +
            "where subject_type=#{subjectType} AND isdelete=0 " +
            "order by createtime desc " +
            "LIMIt #{start},#{pageSize}")
    List<SysSubject> selecttype(Integer start, Integer pageSize, Integer subjectType);

    @Select("select count(*) as total from sys_subject where isdelete=0 and subject_type=#{subjectType} order by createtime desc")
    Integer selecttixing(Integer subjectType);


    @Select({
            "<script>",
            "select count(*) as total ",
            "from sys_subject ",
            "where 1=1 ",
            "<if test='shu != null and shu.size() > 0'>",
            "<foreach item='item' index='index' collection='shu' open='and (' separator=' or ' close=')'>",
            "subject_label LIKE CONCAT('%', #{item}, '%')",
            "</foreach>",
            "</if>",
            "<if test='subjectLv != 0'>",
            "and subject_lv = #{subjectLv}",
            "</if>",
            "<if test='tt != null'>",
            "and subject_type = #{tt}",
            "</if>",
            "<if test='zz != 0'>",
            "and subject_major = #{zz}",
            "</if>",
            "and isdelete = 0 ",
            "</script>"
    })
    Integer selectbiaoqianshu(List<Integer> shu, Integer subjectLv, Integer tt, Integer zz);

    @Select({
            "<script>",
            "select subject_id, subject_name, option_a, option_b, option_c, option_d, option_e, option_f, subject_answer, subject_type, subject_label, subject_major, subject_image, subject_lv, creator, createtime, reviser ",
            "from sys_subject ",
            "where 1=1 ",
            "<if test='shu != null and shu.size() > 0'>",
            "<foreach item='item' index='index' collection='shu' open='and (' separator=' or ' close=')'>",
            "subject_label LIKE CONCAT('%', #{item}, '%')",
            "</foreach>",
            "</if>",
            "<if test='slv != 0'>",
            "and subject_lv = #{slv}",
            "</if>",
            "<if test='tt != null'>",
            "and subject_type = #{tt}",
            "</if>",
            "<if test='zz != 0'>",
            "and subject_major = #{zz}",
            "</if>",
            "and isdelete = 0 ",
            "order by createtime desc ", // 添加排序条件
            "LIMIT #{start}, #{pageSize}",
            "</script>"
    })
    List<SysSubject> selectbiaoqian(Integer start, Integer pageSize, List<Integer> shu, Integer slv, Integer tt, Integer zz);

    @Select({
            "<script>",
            "select subject_id AS subjectId, subject_name, option_a, option_b, option_c, option_d, option_e, option_f, subject_answer, subject_type, subject_label, subject_major, subject_image, subject_lv, creator, createtime, reviser ",
            "from sys_subject ",
            "where 1=1 ",
            "<if test='slv != 0'>",
            "and subject_lv = #{slv}",
            "</if>",
            "<if test='tt != null'>",
            "and subject_type = #{tt}",
            "</if>",
            "<if test='zz != 0'>",
            "and subject_major = #{zz}",
            "</if>",
            "and isdelete = 0 ",
            "order by createtime desc ", // 添加排序条件
            "LIMIT #{start}, #{pageSize}",
            "</script>"
    })
    List<SysSubject> selectxxx(Integer start, Integer pageSize, Integer slv, Integer tt, Integer zz);

    @Select({
            "<script>",
            "select count(*) as total ",
            "from sys_subject ",
            "where 1=1 ",
            "<if test='slv != 0'>",
            "and subject_lv = #{slv}",
            "</if>",
            "<if test='tt != null'>",
            "and subject_type = #{tt}",
            "</if>",
            "<if test='zz != 0'>",
            "and subject_major = #{zz}",
            "</if>",
            "and isdelete = 0 ",
            "</script>"
    })
    Integer selectxxxzong(Integer slv, Integer tt, Integer zz);

    @Select("select direction_id,direction_name from sys_direction ")
    List<Direction> selectzhuanye();
}
